package com.manager.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Administrator
 */
@WebFilter(value = "/manager")
public class Filters implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("执行了");

        //转换类型 获取session
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();
        //获取session值
        String user = (String) session.getAttribute("user");

        //放行
        if (user != null) {
            System.out.println("登陆了");
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        } else {
            System.out.println("没登陆");
            request.getRequestDispatcher("index.jsp").forward(request, servletResponse);
        }

    }

    @Override
    public void destroy() {
    }
}
